home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / comm / ftp4w24b.zip / sample / main2.c < prev    next >
C/C++ Source or Header  |  1995-05-11  |  9KB  |  370 lines

  1.  
  2. /* **************************************************************
  3.  *
  4.  *
  5.  *      C W   :    P r o c e d u r e s
  6.  * 
  7.  *
  8.  * **************************************************************** */
  9.  
  10.  
  11.  
  12. #include <windows.h>
  13. #include <windowsx.h>
  14. #include <stdarg.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19.  
  20. extern HINSTANCE       hInst;             
  21. extern HWND            hwnd;
  22.  
  23.  
  24. /* **************************************************************
  25.  *    G a u g e
  26.  *             B y     Santanu Lahiri (winFTP)
  27.  * **************************************************************/
  28.  
  29.  
  30. static LONG lTotalBytes=0;
  31. static DWORD lFileSize=0;
  32. static LONG lXferBytes=0;
  33. static int  nPercentXfer;
  34. static HWND hWndXfer=NULL;
  35. static LPSTR szXferWnd = "XferWindow";
  36.  
  37.  
  38. HPEN hPenDark;
  39. HPEN hPenLight;
  40. HBRUSH  hbrGray1;
  41. int  nWndx=10;                // the x axis multiplier
  42. int  nWndy=15;                // the y axis multiplier
  43.  
  44.  
  45.  
  46. LRESULT CALLBACK WndXferProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
  47.  
  48.  
  49. int nCwUnregisterClasses(void)
  50. {
  51.   UnregisterClass (szXferWnd, hInst);
  52. return 0;
  53. }
  54.  
  55.  
  56. int nCwRegisterClasses(void)
  57. {
  58. WNDCLASS   wc;    // struct to define a window class
  59.  
  60.   hbrGray1 = CreateSolidBrush (RGB (192,192,192));
  61.   memset(&wc, 0x00, sizeof(wc));
  62.   wc.cbClsExtra = 0;
  63.   wc.cbWndExtra = 0;
  64.   wc.hInstance = hInst;
  65.   wc.hCursor = LoadCursor ((HINSTANCE) NULL, IDC_ARROW);
  66.   wc.style = CS_HREDRAW | CS_VREDRAW;
  67.   wc.lpfnWndProc   = WndXferProc;
  68.   wc.hIcon         = LoadIcon (hInst, IDI_APPLICATION);
  69.   wc.hbrBackground = hbrGray1;
  70.   wc.lpszMenuName  = NULL;      /* Menu Name is App Name */
  71.   wc.lpszClassName = szXferWnd; /* Class Name is App Name */
  72.   if (RegisterClass (&wc)==0) return -1;
  73. return 0;
  74. }
  75.  
  76.  
  77. void CreateButtonPens()
  78. {
  79.   hPenDark  = CreatePen (PS_SOLID, 1, RGB (128,128,128));
  80.   hPenLight = CreatePen (PS_SOLID, 1, RGB (224,224,224));
  81. }
  82.  
  83. void DeleteButtonPens()
  84. {
  85.   DeleteObject (hPenDark);
  86.   DeleteObject (hPenLight);
  87. }
  88.  
  89.  
  90. void BoxIt (HDC hDC, int left, int top, int width, int height, BOOL flag)
  91. {
  92.   POINT Pt;
  93.   HPEN hPenOld;
  94.   
  95.   hPenOld = SelectObject (hDC, flag? hPenDark : hPenLight);
  96.   MoveToEx (hDC, (left*nWndx)/4, ((top+height)*nWndy)/8, &Pt);
  97.   LineTo (hDC, (left*nWndx)/4, (top*nWndy)/8);
  98.   LineTo (hDC, ((left+width)*nWndx)/4, (top*nWndy)/8);
  99.   if (flag) SelectObject (hDC,hPenLight); 
  100.   else SelectObject (hDC,hPenDark);
  101.   LineTo (hDC, ((left+width)*nWndx)/4, ((top+height)*nWndy)/8);
  102.   LineTo (hDC, (left*nWndx)/4, ((top+height)*nWndy)/8);
  103.   SelectObject (hDC, hPenOld);
  104. }
  105.  
  106.  
  107. void PaintXfer (HDC hDC)
  108. {
  109.   RECT rc;
  110.   char szBuf[10];
  111.  
  112.   CreateButtonPens();
  113.   SelectObject (hDC, GetStockObject (WHITE_BRUSH));
  114.   Rectangle (hDC, (nWndx*20)/4, (nWndy*10)/8, (nWndx*122)/4, (nWndy*20)/8);
  115.   rc.left = (nWndx*21)/4;  rc.right = (nWndx*(nPercentXfer+21))/4;
  116.   rc.top  = (nWndy*11)/8;  rc.bottom= (nWndy*19)/8;
  117.   FillRect (hDC, &rc, GetStockObject (GRAY_BRUSH));
  118.   rc.right = (nWndx*121)/4;
  119.   SetBkMode (hDC, TRANSPARENT);
  120.   wsprintf (szBuf, "%d%%", nPercentXfer);
  121.   DrawText (hDC, szBuf, lstrlen (szBuf), &rc, DT_CENTER | DT_VCENTER);
  122.   SetBkMode (hDC, OPAQUE);
  123.   BoxIt (hDC, 18, 8, 106, 14, FALSE);
  124.   DeleteButtonPens();
  125. }
  126.  
  127.  
  128.  
  129. void OnPaintXfer (HWND hWnd)
  130. {
  131.   HDC hDC;
  132.   PAINTSTRUCT ps;
  133.   
  134.   hDC = BeginPaint (hWnd, &ps);
  135.   PaintXfer (hDC);
  136.   EndPaint (hWnd, &ps);
  137. }
  138.  
  139. void SetXmitBytes (LONG lBytes, LONG lTotal)
  140. {
  141.   int nOld;
  142.   
  143.   lXferBytes = lBytes;
  144.   if (lTotalBytes==0)  lTotalBytes = lTotal;
  145.   if ((lTotalBytes==0)||(hWndXfer==NULL)) return;
  146.   nOld = nPercentXfer;
  147.   nPercentXfer = (int) ((lBytes*100)/lTotalBytes);
  148.   if (nOld!=nPercentXfer) 
  149.   {
  150.     HDC hDC;
  151.   
  152.     hDC = GetDC (hWndXfer);
  153.     PaintXfer (hDC);
  154.     ReleaseDC (hWndXfer, hDC);
  155.   }
  156. }
  157.  
  158.  
  159.  
  160. LRESULT CALLBACK WndXferProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  161. {
  162.   switch (Msg)
  163.   {
  164.     case WM_GETMINMAXINFO: 
  165.     {
  166.       MINMAXINFO FAR *lpmmi=(MINMAXINFO FAR *) lParam;
  167.       lpmmi->ptMaxSize.x = (nWndx*140)/4;
  168.       lpmmi->ptMaxSize.y = (nWndy*45)/8;
  169.       lpmmi->ptMaxTrackSize.x = (nWndx*140)/4;
  170.       lpmmi->ptMaxTrackSize.y = (nWndy*45)/8;
  171.       lpmmi->ptMinTrackSize.x = (nWndx*140)/4;
  172.       lpmmi->ptMinTrackSize.y = (nWndy*45)/8;
  173.     } break;
  174.     case WM_PAINT: OnPaintXfer (hWnd); return 0L;
  175.   }
  176.   return (LRESULT) DefWindowProc (hWnd, Msg, wParam, lParam);
  177. }
  178.  
  179.  
  180. void CreateXferWindow()
  181. {
  182.   if (hWndXfer!=NULL) return;
  183.   hWndXfer = CreateWindowEx ( WS_EX_TOPMOST, 
  184.        szXferWnd, "",  WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CAPTION,
  185.        CW_USEDEFAULT, CW_USEDEFAULT, (nWndx*140)/4, (nWndy*40)/8, (HWND) NULL, 
  186.        (HMENU) NULL, hInst,  NULL);
  187.   if (hWndXfer==NULL) MessageBeep (65535u);
  188.   lTotalBytes = 0L;
  189.   lXferBytes  = 0L;
  190.   nPercentXfer = 0L;
  191. }
  192.  
  193.  
  194. void DeleteXferWindow()
  195. {
  196.   if (hWndXfer==NULL) return;
  197.   DestroyWindow (hWndXfer);
  198.   lTotalBytes  = lXferBytes = 0l;
  199.   nPercentXfer = 0;
  200.   hWndXfer = NULL;
  201. }
  202.  
  203. void SetXferWindowText (LPSTR lpStr)
  204. {
  205.   SetWindowText (hWndXfer, lpStr);
  206. }
  207.  
  208. void SetXferTotalBytes (LONG Total)
  209. {
  210.   lTotalBytes = Total;
  211. }
  212.  
  213.  
  214. /* **************************************************************
  215.  *    S c r o l l e r 
  216.  * **************************************************************/
  217.  
  218. extern int  nHorzSiz, nVertSiz;
  219. extern int  nVertPos, nHorzPos;           // scroll pos for debug window
  220. extern int  nHorzMax, nCurMsg;
  221. extern int  nMaxMsg;
  222. extern int  nHorzTab;
  223.  
  224.  
  225. // static HWND hWndViewMsg = (HWND) NULL;
  226. static LPSTR * lpMsg;
  227. static LPSTR szNoMem   = "Unable to allocate memory!";
  228. static LPSTR szZoomErr = "Unable to allocate memory!";
  229. static BOOL bZoomFlag=FALSE;
  230. static BOOL bNewMessage=FALSE;
  231.  
  232.  
  233.  
  234. void DoAddLine (LPSTR lpBuf)
  235. {
  236. LPSTR lp;
  237.  
  238.   // added in some error checking to try to eliminate GPFs
  239.   if (lpBuf==NULL || lpBuf[0]==0)   return;
  240.   if (lpMsg==NULL)
  241.     {
  242.       lpMsg = (LPSTR *) GlobalAllocPtr (GHND, nMaxMsg * sizeof (LPSTR));
  243.       nCurMsg = 0;
  244.       if (lpMsg==NULL) 
  245.         {
  246.           MessageBox (hwnd, szNoMem, szZoomErr, MB_OK);
  247.           return;
  248.         }
  249.     }
  250.   if (nCurMsg>=nMaxMsg)
  251.     {
  252.       GlobalFreePtr (lpMsg[0]);
  253.       memmove (lpMsg, lpMsg+1, (nMaxMsg-1)*sizeof (LPSTR));
  254.       nCurMsg = nMaxMsg-1;
  255.     }
  256.   lp = lpMsg[nCurMsg] = (LPSTR) GlobalAllocPtr (GHND, lstrlen (lpBuf) + 5);
  257.   if (lp!=NULL) 
  258.     {
  259.       HDC hDC;
  260.       SIZE siz;
  261.       
  262.       lstrcpy (lp, lpBuf);
  263.       hDC = GetDC (hwnd);
  264.       GetTextExtentPoint (hDC, lp, lstrlen (lp), &siz);
  265.       ReleaseDC (hwnd, hDC);
  266.       nHorzMax = nHorzMax > siz.cx ? nHorzMax : siz.cx ;
  267.       SetScrollRange (hwnd, SB_HORZ, 0, nHorzMax, TRUE);
  268.       SetScrollRange (hwnd, SB_VERT, 0, nCurMsg, TRUE);
  269.       InvalidateRect (hwnd, NULL, TRUE);
  270.       nCurMsg++;
  271.       bNewMessage = TRUE;
  272.     }  /* lp not NULL */
  273.     else 
  274.     {
  275.       MessageBox (hwnd, szNoMem, szZoomErr, MB_OK);
  276.       return;
  277.     } /* no more memory */
  278. } /* DoAddLine */
  279.  
  280.  
  281. //*************************************************************************
  282. //*************************************************************************
  283. void ReleaseDisplayMem()
  284. {
  285. int nI;
  286.   if (lpMsg!=NULL) 
  287.   {
  288.     for (nI=0; nI<nMaxMsg; nI++) 
  289.     {
  290.       if (lpMsg[nI]!=NULL) GlobalFreePtr (lpMsg[nI]);
  291.       lpMsg[nI] = NULL;
  292.     }
  293.   }
  294.   nCurMsg = nVertPos = nHorzPos = nHorzMax = 0;
  295.   SetScrollPos (hwnd, SB_HORZ, 0, TRUE);
  296.   SetScrollPos (hwnd, SB_VERT, 0, TRUE);
  297.   SetScrollRange (hwnd, SB_HORZ, 0, 1, TRUE);
  298.   SetScrollRange (hwnd, SB_VERT, 0, 1, TRUE);
  299. } /* ReleaseDisplayMemory */
  300.  
  301.  
  302. //*************************************************************************
  303. void DoPaint(HWND hWnd)
  304. {
  305. HDC         hDC;
  306. PAINTSTRUCT ps;
  307. int         nI, nRows, nLine, nPos, nHt;
  308. RECT        rRect;
  309. TEXTMETRIC  tm;
  310.  
  311.   memset (& ps, 0, sizeof ps);  
  312.   hDC = BeginPaint (hWnd, &ps);
  313.   SelectObject (hDC, GetStockObject (ANSI_VAR_FONT));
  314.   // Included as the background is not a pure color
  315.   SetBkMode(hDC, TRANSPARENT);
  316.   GetTextMetrics (hDC,&tm);
  317.   nHt = tm.tmHeight + tm.tmExternalLeading;
  318.   GetClientRect (hWnd, &rRect);
  319.   nHorzSiz = rRect.right;
  320.   nVertSiz = nRows = rRect.bottom/nHt;
  321.  
  322.   if (    nCurMsg>nRows+nVertPos  && bNewMessage)
  323.            nVertPos = nLine = nCurMsg-nRows;
  324.   else     nLine=nVertPos;
  325.  
  326.   if (lpMsg!=NULL)
  327.   {
  328.     for (nI=nPos=0; (nI<nRows) && (nLine<nCurMsg); nI++, nLine++, nPos+=nHt) 
  329.     {
  330.       if (lpMsg[nLine]!=NULL) 
  331.       {
  332.         TextOut (hDC, 20-nHorzPos, nPos, lpMsg[nLine], lstrlen (lpMsg[nLine]));
  333.       }
  334.     }
  335.   }
  336.   EndPaint(hWnd, &ps);
  337.   bNewMessage=FALSE;
  338. } /* DoPaint */
  339.  
  340.  
  341.  
  342. int Ecris(const char *szFormat,...)
  343. {
  344. va_list     vaArgs;
  345. static char szBuf[512];
  346. char       *p, *q;
  347.  
  348.    va_start(vaArgs,szFormat);
  349.    if (vsprintf (szBuf,szFormat,vaArgs)!=EOF) 
  350.      {
  351.         for (q=p=szBuf ;  *p!=0 ; p++)
  352.          {
  353.            if (*p=='\r')   if (*(p+1)=='\n')  *p=0;
  354.            if (*p=='\n')   
  355.                 {
  356.                  *p=0;
  357.                   DoAddLine (q);
  358.                   q=p+1;
  359.                 }
  360.          }
  361.         DoAddLine (q);
  362.      }
  363.    va_end(vaArgs);
  364. return 0;
  365. } /* Ecris */
  366.  
  367.  
  368.  
  369.  
  370.